//
//  EAppInfo.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-8.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

#import "EAppInfo.h"
#import "mcf.h"
#import "JSONKit.h"

@implementation EAppInfo

@synthesize appId = appId_;
@synthesize supportedDevices = supportedDevices_;

@synthesize artworkUrl = artworkUrl_;
@synthesize version = version_;

@synthesize appDescription = appDescription_;
@synthesize price = price_;

@synthesize releaseDate = releaseDate_;
@synthesize releaseNote = releaseNote_;

@synthesize sellerName = sellerName_;
@synthesize currentcy = currentcy_;

@synthesize appName = appName_;
@synthesize primaryGenreName = primaryGenreName_;

@synthesize trackCensoredName = trackCensoredName_;
@synthesize currentAdvisoryRating = currentAdvisoryRating_;

@synthesize fileSize = fileSize_;
@synthesize sellerUrl = sellerUrl_;

@synthesize formattedPrice = formattedPrice_;
@synthesize averageUserRatingForCurrentVersion = averageUserRatingForCurrentVersion_;

@synthesize userRationCountForCurrentVersion = userRationCountForCurrentVersion_;
@synthesize trackContentRating = trackContentRating_;

@synthesize averageUserRating = averageUserRating_;
@synthesize userRatingCount = userRatingCount_;

@synthesize free = free_;

- (void) dealloc
{
    YRelease(appId_);
    YRelease(supportedDevices_)
    
    YRelease(artworkUrl_)
    YRelease(version_)
    
    YRelease(appDescription_)
    YRelease(releaseDate_)
    
    YRelease(releaseNote_)
    YRelease(sellerName_)
    
    YRelease(currentcy_)
    YRelease(appName_)
    
    YRelease(primaryGenreName_)
    YRelease(trackContentRating_)
    
    YRelease(releaseNote_)
    YRelease(trackCensoredName_)
    
    YRelease(currentAdvisoryRating_)
    YRelease(sellerUrl_)
    
    YRelease(formattedPrice_)
    YRelease(averageUserRatingForCurrentVersion_)
    
    YRelease(trackContentRating_)
    
    
    
    [super dealloc];
}

- (id) initWithJsonData:(NSData *) data
{
    if (!data)
    {
        return nil;
    }
        
    self = [super init];
    if (self)
    {
        [self parseJsonData:data];
    }
    
    return self;
}

- (BOOL) isForiPad
{
    /**
     iPad23G,
     iPad3G,
     iPad2Wifi,
     iPadWifi
     */
    BOOL isAll = [self.supportedDevices containsObject:@"iPad23G"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad3G"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad2Wifi"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPadWifi"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad3Wifi"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad33G"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad4Wifi"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad43G"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad5Wifi"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad53G"];
    if (isAll)
    {
        return YES;
    }
    
    
    return NO;
}

- (BOOL) isForiPhone
{
    /* ["iPhone-3GS", "iPad2Wifi", "iPad3G", "iPhone4", "iPad23G", "iPodTouchThirdGen", "iPodTouchourthGen", "iPadWifi"] */
    BOOL isAll = [self.supportedDevices containsObject:@"iPhone-3GS"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPhone4"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPad23G"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPhone5"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPhone6"];
    if (isAll)
    {
        return YES;
    }
    
    isAll = [self.supportedDevices containsObject:@"iPhone5S"];
    if (isAll)
    {
        return YES;
    }
    
    return YES;
}


- (BOOL) isForiPhoneAndiPad
{
    BOOL isAll = [self.supportedDevices containsObject:@"all"];
    if (isAll)
    {
        return YES;
    }
    
    return [self isForiPad] && [self isForiPhone];
}


- (BOOL) isFree
{
    return self.price == 0.0f ? YES : NO;
}

- (void) parseJsonData:(NSData *) data
{
    if (!data)
    {
        return;
    }
    
    NSDictionary * result = [data objectFromJSONData];
    if ([NSDictionary isNilOrEmpty:result])
    {
        return;
    }
    
    id appsNode = [result objectForKey:@"results"];
    if (!appsNode || [NSArray isNilOrEmpty:appsNode])
    {
        return;
    }
    
    NSDictionary * appNode = [appsNode objectAtIndex:0];
    
    self.appId = [NSString stringWithFormat:@"%@", [appNode objectForKey:@"trackId"]];
    self.supportedDevices = [appNode objectForKey:@"supportedDevices"];
    
    self.artworkUrl = [appNode objectForKey:@"artworkUrl"];
    if([NSString isNilOrEmpty:self.artworkUrl])
    {
        self.artworkUrl = [appNode objectForKey:@"artworkUrl100"];
    }
    
    if([NSString isNilOrEmpty:self.artworkUrl])
    {
        self.artworkUrl = [appNode objectForKey:@"artworkUrl512"];
    }
    
    if([NSString isNilOrEmpty:self.artworkUrl])
    {
        self.artworkUrl = [appNode objectForKey:@"artworkUrl160"];
    }        
    
    self.version = [appNode objectForKey:@"version"];
    
    self.appDescription = [appNode objectForKey:@"description"];
    self.price =  [[appNode objectForKey:@"price"] floatValue];
    
    self.releaseDate = [appNode objectForKey:@"releaseDate"];
    self.sellerName = [appNode objectForKey:@"sellerName"];
    
    self.currentcy = [appNode objectForKey:@"currentcy"];
    self.appName = [appNode objectForKey:@"trackName"];
    
    self.primaryGenreName = [appNode objectForKey:@"primaryGenreName"];
    self.releaseNote = [appNode objectForKey:@"releaseNote"];
    
    self.trackCensoredName = [appNode objectForKey:@"trackCensoredName"];
    self.currentAdvisoryRating = [appNode objectForKey:@"currentAdvisoryRating"];
    
    self.fileSize = [[appNode objectForKey:@"fileSizeBytes"] longLongValue];
    self.sellerUrl = [appNode objectForKey:@"sellerUrl"];
    
    self.formattedPrice = [appNode objectForKey:@"formattedPrice"];
    self.averageUserRatingForCurrentVersion = [appNode objectForKey:@"averageUserRatingForCurrentVersion"];
    
    self.userRationCountForCurrentVersion = [[appNode objectForKey:@"userRationCountForCurrentVersion"] integerValue];
    
    self.userRatingCount = [[appNode objectForKey:@"userRatingCount"] integerValue];
}


@end
